//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.core
{
	import flash.utils.Dictionary;
	
	import reader.view.components.TypedWindow;
	
	public class WindowManager
	{
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		private static var windowsPool:Dictionary = new Dictionary();
		
		////////////////////////////////////////////////////////////////////////////////
		//methods
		public static function isWindowRegisted(windowType:String):Boolean
		{
			return windowsPool[windowType] != undefined;
		}
		
		public static function getRegisterWindow(windowType:String):TypedWindow
		{
			return windowsPool[windowType];
		}
		
		public static function eachWindow(callFunction:Function):void
		{
			for each(var w:TypedWindow in windowsPool)
			{
				callFunction(w);
			}
		}
		
		public static function registerWindow(window:TypedWindow):void
		{			
			if(!isWindowRegisted(window.windowType))
			{
				windowsPool[window.windowType] = window;
			}
			else
			{
				throw new ReaderError(ReaderError.WINDOW_OPENED_ERROR);
			}
				
		}
		
		public static function unRegisterWindow(window:TypedWindow):void
		{
			if(isWindowRegisted(window.windowType))
			{
				delete windowsPool[window.windowType];
			}
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//Single-ton setting
		private static var instance:WindowManager;
			
		public function WindowManager(access:Privace)
		{
			if(access)
				if(instance == null) instance = this;
			else throw new ReaderError(ReaderError.MANAGER_ERROR);
		}	
			
		public static function get application():WindowManager
		{
			if(instance == null) instance = new WindowManager(new Privace);
			return instance;
		}

	}
}
class Privace{}